﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Configuration;

namespace blackhouse.BlackApart.Web {
	public class ImageSizeUtils {

		#region Fields

		private static bool set = false;
		private static Size furnitureSize = new Size(100, 100);
		private static Size apartmentPhotoSmall = new Size(100, 39);
		private static Size apartmentPhotoFull = new Size(1600, 1200);
		private static Size apartmentPhotoReservation = new Size(152, 114);
		private static Size apartmentBanner = new Size(531, 279);

		#endregion

		#region Properties

		/// <summary>
		/// Size of furniture icon. Default value is 100x100. You can change this by setting the value for appSetting["FurnitureSize"]
		/// </summary>
		public static Size FurnitureSize {
			get {
				Set();
				return furnitureSize;
			}
			private set {
				Set();
				furnitureSize = value;
			}
		}

		/// <summary>
		/// Size of apartment photo in small variant. Default value is 100x39. You can change this by setting the value for appSetting["ApartmentPhotoSmall"]
		/// </summary>
		public static Size ApartmentPhotoSmall {
			get {
				Set();
				return apartmentPhotoSmall;
			}
			private set {
				Set();
				apartmentPhotoSmall = value;
			}
		}

		/// <summary>
		/// Size of apartment photo in small variant. Default value is 1600x1200. You can change this by setting the value for appSetting["ApartmentPhotoFull"]
		/// </summary>
		public static Size ApartmentPhotoFull {
			get {
				Set();
				return apartmentPhotoFull;
			}
			private set {
				Set();
				apartmentPhotoFull = value;
			}
		}

		/// <summary>
		/// Size of apartment photo displayed on reservation variants. Default value is 152x114. You can change this by setting the value for appSetting["ApartmentPhotoReservation"]
		/// </summary>
		public static Size ApartmentPhotoReservation {
			get {
				Set();
				return apartmentPhotoReservation;
			}
			private set {
				Set();
				apartmentPhotoReservation = value;
			}
		}

		/// <summary>
		/// Size of apartment on promotion banner . Default value is 531x279. You can change this by setting the value for appSetting["ApartmentBannerImageSize"]
		/// </summary>
		public static Size ApartmentBanner {
			get {
				Set();
				return apartmentBanner;
			}
			private set {
				Set();
				apartmentBanner = value;
			}
		}

		#endregion

		#region Methods

		private static void Set() {
			if (!set) {
				furnitureSize = ParseSize(ConfigurationManager.AppSettings["FurnitureSize"], furnitureSize);
				apartmentPhotoSmall = ParseSize(ConfigurationManager.AppSettings["ApartmentPhotoSmall"], apartmentPhotoSmall);
				apartmentPhotoFull = ParseSize(ConfigurationManager.AppSettings["ApartmentPhotoFull"], apartmentPhotoFull);
				apartmentPhotoReservation = ParseSize(ConfigurationManager.AppSettings["ApartmentPhotoReservation"], apartmentPhotoReservation);
				apartmentBanner = ParseSize(ConfigurationManager.AppSettings["ApartmentBannerImageSize"], apartmentBanner);
			}
		}

		private static Size ParseSize(string sSize, Size defaultSize) {
			if (string.IsNullOrEmpty(sSize)) return defaultSize;
			string[] sParts = sSize.Split('x');
			int width, height;
			if (
				Int32.TryParse(sParts[0], out width)
				&& Int32.TryParse(sParts[1], out height)) {
				return new Size(width, height);
			}
			else return defaultSize;
		}

		#endregion

	}
}